﻿using System;
using System.Threading;
using Styx.Common;
using Styx.CommonBot;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Action = Styx.TreeSharp.Action;

namespace TuanHA_Monk
{
    public partial class Classname
    {
        #region Brewmaster Rotation

        //public DateTime LastPendingSpellS = new DateTime(1970, 1, 1);

        private Composite BrewmasterRotation()
        {
            return new PrioritySelector(
                new Action(delegate
                               {
                                   if (THSettings.Instance.SAutoTarget && !Me.Mounted &&
                                       Me.CurrentTarget != null && UnitAttack != null &&
                                       Me.CurrentTarget != UnitAttack &&
                                       UnitAttack.Combat && //Make sure don't aggro more
                                       (Me.CurrentTarget.Distance - 5f > UnitAttack.Distance ||
                                        !Me.CurrentTarget.InLineOfSight ||
                                        !Me.CurrentTarget.IsAlive ||
                                        !Me.CurrentTarget.Combat && Me.Combat))
                                   {
                                       Logging.Write(LogLevel.Diagnostic, "Select new Target");
                                       UnitAttack.Target();
                                   }

                                   //target the lowest threat unit
                                   if (THSettings.Instance.SAutoTarget && Me.CurrentTarget != null &&
                                       Me.CurrentTarget.IsValid && GetUnitLowThreat() &&
                                       UnitLowThreat.IsValid && Me.CurrentTarget != UnitLowThreat &&
                                       Me.CurrentTarget.ThreatInfo.RawPercent > UnitLowThreat.ThreatInfo.RawPercent + 5)
                                   {
                                       UnitLowThreat.Target();
                                       Logging.Write(LogLevel.Diagnostic, "Switch to Lowest Threat Target");
                                   }
                                   //target the lowest threat unit
                                   if (THSettings.Instance.SAutoTarget && Me.CurrentTarget == null &&
                                       GetUnitLowThreat() &&
                                       UnitLowThreat.IsValid)
                                   {
                                       UnitLowThreat.Target();
                                       Logging.Write(LogLevel.Diagnostic, "Target  Lowest Threat Unit");
                                   }

                                   //Hold dps on Dungeon or Raid
                                   if ((Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid) &&
                                       Me.CurrentTarget != null &&
                                       !Me.CurrentTarget.Combat)
                                   {
                                       return RunStatus.Success;
                                   }

                                   return RunStatus.Failure;
                               }),
                //Starting the movement right here
                //But first, facing target
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) &&
                    IsEnemy(Me.CurrentTarget) &&
                    !Me.IsFacing(Me.CurrentTarget) &&
                    Me.CurrentTarget.InLineOfSight &&
                    !IsOverrideModeOn,
                    new Action(delegate
                                   {
                                       Me.CurrentTarget.Face();
                                       return RunStatus.Failure;
                                   })
                    ),
                new Decorator(
                    ret =>
                    Me.Mounted,
                    new Action(delegate { return RunStatus.Success; })
                    ),
                //DPS Rotation Here
                //Out of Dungeon/Raid Clash
                new Decorator(
                    ret =>
                    THSettings.Instance.SClash &&
                    !Me.IsFlying &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) && //!Me.IsSwimming &&
                    !CastingorGCDL() && Me.CurrentTarget.Distance >= 8 &&
                    Me.CurrentTarget.Distance < 40 &&
                    !Me.CurrentMap.IsDungeon && !Me.CurrentMap.IsRaid &&
                    Me.CurrentTarget.InLineOfSight &&
                    SpellManager.HasSpell("Clash") &&
                    SpellManager.Spells["Clash"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Clash", Me.CurrentTarget);
                                       return RunStatus.Failure;
                                   })
                    ),
                new Decorator(
                    ret =>
                    THSettings.Instance.SRoll &&
                    !Me.IsFlying &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) && Me.GetAuraById(119085) == null && //119085 Momentum
                    !CastingorGCDL() &&
                    //Need distance > 15 or you will roll out of melee range
                    Me.CurrentTarget.Distance > 15 &&
                    Me.CurrentTarget.Distance < 40 &&
                    Me.CurrentTarget.InLineOfSight &&
                    SpellManager.HasSpell("Roll") &&
                    SpellManager.Spells["Roll"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       if (!Me.IsSafelyFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                           return RunStatus.Running;
                                       }
                                       CastSpell("Roll", Me.CurrentTarget);
                                       return RunStatus.Failure;
                                   })
                    ),
                new Decorator(
                    ret =>
                    THSettings.Instance.SProvoke &&
                    !Me.IsFlying &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    !Me.IsInInstance &&
                    !CastingorGCDL() && !Me.CurrentTarget.IsWithinMeleeRange &&
                    Me.CurrentTarget.Distance < 38 &&
                    Me.CurrentTarget.InLineOfSight &&
                    //Only provoke target that have no target or not targeting me
                    !Me.CurrentTarget.IsTargetingMeOrPet &&
                    SpellManager.HasSpell("Provoke") &&
                    //Only provoke when not Clashing, rolling or roll on cooldown
                    LastCastSpell != "Clash" &&
                    LastCastSpell != "Roll" &&
                    SpellManager.Spells["Provoke"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       CastSpell("Provoke", Me.CurrentTarget);
                                       return RunStatus.Failure;
                                   })
                    ),
                //Dungeon and Raid Provoke
                new Decorator(
                    ret =>
                    THSettings.Instance.SProvoke &&
                    !Me.IsFlying &&
                    GetUnitProvoke() && UnitProvoke.IsValid &&
                    SpellManager.HasSpell("Provoke") &&
                    SpellManager.Spells["Provoke"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       CastSpell("Provoke", UnitProvoke);
                                       return RunStatus.Failure;
                                   })
                    ),
                //Inside Dungeon/Raid Leer of the Ox, GetUnitUnitLeeroftheOx
                new Decorator(
                    ret =>
                    THSettings.Instance.SLeeroftheOx &&
                    LastCastSpell != "Provoke" &&
                    !Me.IsFlying &&
                    GetUnitUnitLeeroftheOx() && UnitLeeroftheOx.IsValid &&
                    !CastingorGCDL() &&
                    SpellManager.HasSpell("Leer of the Ox") &&
                    SpellManager.Spells["Leer of the Ox"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       CastSpell("Leer of the Ox", UnitLeeroftheOx);
                                       return RunStatus.Failure;
                                   })
                    ),
                //Inside Dungeon/Raid Clash, act like Warrior Intervene
                new Decorator(
                    ret =>
                    LastCastSpell != "Provoke" &&
                    THSettings.Instance.SClash &&
                    !Me.IsFlying &&
                    GetUnitProvoke() && UnitProvoke.IsValid &&
                    SpellManager.HasSpell("Clash") &&
                    SpellManager.Spells["Clash"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(UnitProvoke))
                                       {
                                           return RunStatus.Running;
                                       }
                                       CastSpell("Clash", UnitProvoke);
                                       return RunStatus.Failure;
                                   })
                    ),
                new Decorator(
                    ret =>
                    THSettings.Instance.SSpearHandStrike &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    Me.CurrentTarget.IsWithinMeleeRange &&
                    Me.CurrentTarget.IsCasting && Me.CurrentTarget.CanInterruptCurrentSpellCast &&
                    (Me.CurrentEnergy >= 30 || SpellManager.HasSpell("Brewmaster Training")) &&
                    SpellManager.HasSpell("Spear Hand Strike") &&
                    SpellManager.Spells["Spear Hand Strike"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Spear Hand Strike", Me.CurrentTarget);
                                       return RunStatus.Failure;
                                   })
                    ),
                //Purifying Brew Clear Light Stagger
                new Decorator(
                    ret =>
                    Me.CurrentChi >= 1 &&
                    (Me.HasAura("Light Stagger") ||
                     Me.HasAura("Moderate Stagger") ||
                     Me.HasAura("Heavy Stagger")) &&
                    !Me.Combat &&
                    SpellManager.HasSpell("Purifying Brew"),
                    new Action(delegate
                                   {
                                       CastSpell("Purifying Brew", Me);
                                       return RunStatus.Failure;
                                   })
                    ),
                //Purifying Brew Clear Moderate Stagger or Heavy Stagger
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.IsWithinMeleeRange &&
                    Me.CurrentChi >= 1 &&
                    (Me.HasAura("Moderate Stagger") ||
                     Me.HasAura("Heavy Stagger")) &&
                    Me.Combat &&
                    SpellManager.HasSpell("Purifying Brew"),
                    new Action(delegate
                                   {
                                       CastSpell("Purifying Brew", Me);
                                       return RunStatus.Failure;
                                   })
                    ),
                //[Elusive Brew]
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.IsWithinMeleeRange &&
                    Me.HealthPercent < 90 &&
                    //128936 is the buff 115308 is the dodge use at 7 because elusive on 9 sec CD
                    Me.HasAura(128939) &&
                    Me.GetAuraById(128939).StackCount >= 7 &&
                    //make sure the dodge effect not there
                    !Me.HasAura(115308) &&
                    SpellManager.Spells["Elusive Brew"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       CastSpell("Elusive Brew", Me);
                                       return RunStatus.Failure;
                                   })
                    ),
                //Invoke Xuen, the White Tiger
                new Decorator(
                    ret =>
                    THSettings.Instance.SXuen &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.CurrentTarget.IsWithinMeleeRange &&
                    Me.Combat && Me.HealthPercent < THSettings.Instance.SUrgentHeal &&
                    Me.CurrentTarget.CurrentHealth > Me.CurrentHealth - (Me.CurrentHealth*0.2) &&
                    (Me.CurrentTarget.IsPlayer ||
                     Me.CurrentTarget.MaxHealth > Me.MaxHealth*10 && Me.HealthPercent < THSettings.Instance.SUrgentHeal) &&
                    SpellManager.HasSpell("Invoke Xuen, the White Tiger") &&
                    SpellManager.Spells["Invoke Xuen, the White Tiger"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       CastSpell("Invoke Xuen, the White Tiger", Me.CurrentTarget);
                                       return RunStatus.Failure;
                                   })),
                new Decorator(
                    ret =>
                    THSettings.Instance.STouchofDeath &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.IsWithinMeleeRange &&
                    (Me.CurrentChi >= 3 || HasGlyph.Contains("Glyph of Touch of Death")) &&
                    Me.CurrentTarget.CurrentHealth > Me.CurrentHealth - (Me.CurrentHealth*0.2) &&
                    SpellManager.HasSpell("Touch of Death") && Me.HasAura("Death Note"),
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Touch of Death", Me.CurrentTarget);
                                   })
                    ),
                new Decorator(
                    ret =>
                    THSettings.Instance.SGuard &&
                    LastCastSpell != "Guard" && //tring to fix until HB fix the cooldown issue
                    !CastingorGCDL() &&
                    Me.Combat &&
                    Me.CurrentChi >= 2 &&
                    //Me.HasAura("Tiger Power") &&
                    //Me.GetAuraByName("Tiger Power").StackCount > 2 &&
                    SpellManager.HasSpell("Guard") &&
                    SpellManager.Spells["Guard"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate { CastSpell("Guard", Me); })
                    ),
                //Blackout Kick to maintain Shuffle
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    //only in Dungeon or Raid
                    (Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.IsWithinMeleeRange &&
                    Me.CurrentChi >= 2 &&
                    //only when have Guard
                    //Me.HasAura("Tiger Power") &&
                    //Me.GetAuraByName("Tiger Power").StackCount > 2 &&
                    //renew Shuffle at 2 second left
                    SpellManager.HasSpell("Brewmaster Training") &&
                    (MyAuraTimeLeft("Shuffle", Me) < 10000) &&
                    SpellManager.HasSpell("Blackout Kick"),
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Blackout Kick", Me.CurrentTarget);
                                       //Logging.Write(LogLevel.Diagnostic, "Blackout Kick Shuffle");
                                   })
                    ),
                // [Dampen Harm]
                new Decorator(
                    ret =>
                    LastCastSpell != "Dampen Harm" && //tring to fix until HB fix the cooldown issue
                    Me.Combat &&
                    !CastingorGCDL() &&
                    Me.HealthPercent < THSettings.Instance.SUrgentHeal &&
                    HasTalent.Contains("Dampen Harm") &&
                    SpellManager.Spells["Dampen Harm"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate { CastSpell("Dampen Harm", Me); })
                    ),
                new Decorator(
                    ret =>
                    THSettings.Instance.SSpearHandStrike &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.CurrentTarget.IsWithinMeleeRange &&
                    //use to interrupt or survive
                    (Me.CurrentTarget.IsCasting && LastCastSpell != "Spear Hand Strike" ||
                     Me.HealthPercent < THSettings.Instance.SUrgentHeal) &&
                    Me.CurrentEnergy >= 30 &&
                    HasTalent.Contains("Leg Sweep") &&
                    SpellManager.Spells["Leg Sweep"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate { CastSpell("Leg Sweep", Me.CurrentTarget); })
                    ),
                //Summon Black Ox Statue move to lower priority, need build threat first
                new Decorator(
                    ret =>
                    THSettings.Instance.SSummonBlackOxStatue &&
                    LastDropStatueAttempt.AddSeconds(10) < DateTime.Now &&
                    LastCastSpell != "Summon Black Ox Statue" &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.CurrentTarget.IsWithinMeleeRange &&
                    !GetMyStatue() &&
                    !CastingorGCDL() &&
                    Me.Combat &&
                    //Me.CurrentChi >= 2 && !Me.HasAura("Guard") &&
                    SpellManager.HasSpell("Summon Black Ox Statue") &&
                    SpellManager.Spells["Summon Black Ox Statue"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       LastDropStatueAttempt = DateTime.Now;
                                       CastSpell("Summon Black Ox Statue", Me);
                                       Thread.Sleep(TimeSpan.FromMilliseconds(100));
                                       SpellManager.ClickRemoteLocation(Me.Location);
                                       //Logging.Write(LogLevel.Diagnostic, "Me.Guid:"+ Me.Guid);
                                       LastStatueLocation = Me.Location;
                                   })
                    ),
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Sturdy Ox") &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    !CastingorGCDL() &&
                    //damn boss big foot lol
                    (Me.CurrentTarget.IsWithinMeleeRange || Me.CurrentTarget.Distance <= 8) &&
                    Me.CurrentChi <= Me.MaxChi - 2 &&
                    Me.EnergyPercent > 40 &&
                    SpellManager.HasSpell("Keg Smash") &&
                    SpellManager.Spells["Keg Smash"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Keg Smash", Me.CurrentTarget);
                                       return RunStatus.Success;
                                   })
                    ),
                new Decorator(
                    ret =>
                    !CastingorGCDL() &&
                    Me.Combat &&
                    Me.HealthPercent < THSettings.Instance.SPriorityHeal &&
                    HasTalent.Contains("Zen Sphere") && Me.CurrentChi >= 2 &&
                    SpellManager.Spells["Zen Sphere"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate { CastSpell("Zen Sphere", Me); })
                    ),
                //Heal > AOE DPS, dead = no dps
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.Distance < 40 &&
                    Me.CurrentTarget.InLineOfSpellSight &&
                    Me.HealthPercent < THSettings.Instance.SPriorityHeal &&
                    HasTalent.Contains("Chi Wave") && Me.CurrentChi >= 2 &&
                    SpellManager.Spells["Chi Wave"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Chi Wave", Me.CurrentTarget);
                                   })
                    ),
                //Survie > AOE DPS, dead = no dps
                new Decorator(
                    ret =>
                    //Fix Expel Harm cooldown till HonorBuddy do it
                    LastCastSpell != "Expel Harm" &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.IsWithinMeleeRange &&
                    Me.HealthPercent < 80 &&
                    Me.CurrentEnergy >= 40 && Me.CurrentChi <= Me.MaxChi - 1 &&
                    SpellManager.HasSpell("Expel Harm") &&
                    SpellManager.Spells["Expel Harm"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate { CastSpell("Expel Harm", Me); })
                    ),
                //[Dizzying Haze Off Target in Dungeon]
                new Decorator(
                    ret =>
                    THSettings.Instance.SDizzyingHaze &&
                    //add 5 second delay to Dizzying Haze
                    //Now > LastDizzyingHazeTime + TimeSpan.FromSeconds(5) &&
                    Me.CurrentPendingCursorSpell == null &&
                    GetUnitDizzyingHaze() && UnitDizzyingHaze.IsValid &&
                    Me.HasAura("Stance of the Sturdy Ox") &&
                    !CastingorGCDL() &&
                    Me.CurrentEnergy >= 60 &&
                    SpellManager.HasSpell("Dizzying Haze"),
                    new Action(delegate
                                   {
                                       CastSpell("Dizzying Haze", UnitDizzyingHaze);
                                       Thread.Sleep(TimeSpan.FromMilliseconds(100));
                                       //if (!Me.HasPendingSpell("Dizzying Haze"))
                                       //{
                                       //    return RunStatus.Running;
                                       //}
                                       ObjectManager.Update();
                                       SpellManager.ClickRemoteLocation(UnitDizzyingHaze.Location);
                                       Blacklist.Add(UnitDizzyingHaze.Guid, TimeSpan.FromSeconds(10));
                                       return RunStatus.Success;
                                   })
                    ),
                //[Dizzying Haze CurrentTarget]
                new Decorator(
                    ret =>
                    THSettings.Instance.SDizzyingHaze &&
                    //add 5 second delay to Dizzying Haze
                    //Now > LastDizzyingHazeTime + TimeSpan.FromSeconds(5) &&
                    Me.CurrentPendingCursorSpell == null &&
                    //Don't need Dizzying Haze CurrentTarget if in Dungeon/Raid GetUnitDizzyingHaze in charge here
                    !Me.CurrentMap.IsDungeon && Me.CurrentMap.IsRaid &&
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    !Blacklist.Contains(Me.CurrentTarget.Guid) &&
                    Me.HasAura("Stance of the Sturdy Ox") &&
                    !CastingorGCDL() && Me.CurrentTarget.Distance < 40 &&
                    Me.CurrentEnergy >= 60 &&
                    Me.CurrentTarget.Combat &&
                    (Me.CurrentTarget.IsTargetingMyPartyMember || Me.CurrentTarget.IsTargetingMyRaidMember ||
                     Me.CurrentTarget.IsTargetingMeOrPet) &&
                    //Me.CurrentTarget.GetAuraById(116330) == null &&
                    !Me.CurrentTarget.HasAura("Dizzying Haze") &&
                    SpellManager.HasSpell("Dizzying Haze"),
                    new Action(delegate
                                   {
                                       CastSpell("Dizzying Haze", Me.CurrentTarget);
                                       Thread.Sleep(TimeSpan.FromMilliseconds(100));
                                       ObjectManager.Update();
                                       SpellManager.ClickRemoteLocation(Me.CurrentTarget.Location);
                                       Blacklist.Add(Me.CurrentTarget.Guid, TimeSpan.FromSeconds(10));
                                       return RunStatus.Success;
                                   })
                    ),
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.IsWithinMeleeRange &&
                    Me.CurrentEnergy >= 15 &&
                    (Me.CurrentTarget.IsPlayer || Me.CurrentTarget.Fleeing) &&
                    Me.CurrentTarget.MovementInfo.RunSpeed > 3.5 &&
                    SpellManager.HasSpell("Disable"),
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Disable", Me.CurrentTarget);
                                   })
                    ),
                // [Breath of Fire]
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Sturdy Ox") &&
                    !CastingorGCDL() &&
                    Me.CurrentChi >= 2 &&
                    GetUnitBreathofFire() && UnitBreathofFire.IsValid &&
                    (!Me.IsMoving || Me.IsMoving && Me.IsFacing(UnitBreathofFire)) &&
                    SpellManager.HasSpell("Breath of Fire"),
                    new Action(delegate
                                   {
                                       if (!Me.IsSafelyFacing(UnitBreathofFire))
                                       {
                                           UnitBreathofFire.Face();
                                           return RunStatus.Running;
                                       }
                                       CastSpell("Breath of Fire", UnitBreathofFire);
                                       return RunStatus.Success;
                                   })
                    ),
                new Decorator(
                    ret =>
                    !CastingorGCDL() &&
                    Me.CurrentEnergy >= 60 && Me.CurrentChi < Me.MaxChi &&
                    (CountEneyNeary(Me, 8) >= THSettings.Instance.SSpinningCraneKick + 4 ||
                     CountEneyNeary(Me, 8) >= THSettings.Instance.SSpinningCraneKick &&
                     Me.HasAura("Tiger Power") &&
                     Me.GetAuraByName("Tiger Power").TimeLeft.TotalSeconds > 5 &&
                     Me.GetAuraByName("Tiger Power").StackCount > 2) &&
                    SpellManager.HasSpell("Spinning Crane Kick"),
                    new Action(delegate { CastSpell("Spinning Crane Kick", Me); })
                    ),
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.IsWithinMeleeRange &&
                    (Me.CurrentChi <= Me.MaxChi - 2 || !SpellManager.HasSpell("Tiger Palm")) &&
                    //!SpellManager.HasSpell("Tiger Palm") for low level
                    Me.EnergyPercent > 40 &&
                    SpellManager.HasSpell("Jab"),
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Jab", Me.CurrentTarget);
                                   })
                    ),
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.IsWithinMeleeRange &&
                    //!SpellManager.HasSpell("Blackout Kick") for low level
                    (MyAuraStackCount("Tiger Power", Me) < 3 ||
                     //Renew Tiger Power if target about to die
                     MyAuraTimeLeft("Tiger Power", Me) < 10000 &&
                     Me.CurrentTarget.CurrentHealth < Me.MaxHealth/4 ||
                     MyAuraTimeLeft("Tiger Power", Me) < 4000) &&
                    (Me.CurrentChi >= 3 || SpellManager.HasSpell("Brewmaster Training")) &&
                    SpellManager.HasSpell("Tiger Palm"),
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Tiger Palm", Me.CurrentTarget);
                                   })
                    ),
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.IsWithinMeleeRange &&
                    //Well better Blackout Kick when there's nothing else to do at Max Chi -1 only
                    Me.CurrentChi >= 2 &&
                    SpellManager.HasSpell("Blackout Kick"),
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Blackout Kick", Me.CurrentTarget);
                                   })
                    ),
                //Tiger Palm when nothing else to do
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) && IsEnemy(Me.CurrentTarget) &&
                    Me.IsFacing(Me.CurrentTarget) &&
                    !CastingorGCDL() && Me.CurrentTarget.IsWithinMeleeRange &&
                    SpellManager.HasSpell("Brewmaster Training"),
                    new Action(delegate
                                   {
                                       if (!Me.IsFacing(Me.CurrentTarget))
                                       {
                                           Me.CurrentTarget.Face();
                                       }
                                       CastSpell("Tiger Palm", Me.CurrentTarget);
                                   })
                    ),
                new Decorator(
                    ret =>
                    THSettings.Instance.SBuff &&
                    LastCastSpell != "Legacy of the Emperor" &&
                    !Me.Mounted &&
                    Me.CurrentEnergy >= 95 &&
                    !CastingorGCDL() &&
                    !Me.HasAura("Legacy of the Emperor") &&
                    !Me.HasAura("Blessing of Kings") &&
                    !Me.HasAura("Mark of the Wild") &&
                    //!Me.HasAura("Embrace of the Shale Spider") &&
                    Me.EnergyPercent > 50 &&
                    SpellManager.HasSpell("Legacy of the Emperor"),
                    new Action(delegate { CastSpell("Legacy of the Emperor", Me); })
                    ),
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    !CastingorGCDL() &&
                    !Me.HasAura("Stance of the Sturdy Ox"),
                    new Action(delegate { CastSpell("Stance of the Sturdy Ox", Me); })
                    ),
                //Make sure facing problem gone forever
                new Decorator(
                    ret =>
                    ValidUnit(Me.CurrentTarget) &&
                    IsEnemy(Me.CurrentTarget) &&
                    !Me.IsFacing(Me.CurrentTarget) &&
                    Me.CurrentTarget.InLineOfSight &&
                    !IsOverrideModeOn,
                    new Action(delegate
                                   {
                                       Me.CurrentTarget.Face();
                                       return RunStatus.Failure;
                                   })
                    )
                );
        }

        #endregion
    }
}